package com.yachang.qym.usbhost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.model.UserModel;
import com.yachang.qym.usbhost.utils.LoadImage;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by qinyuanmao on 16/9/12.
 */
public class UserAdapter extends BaseAdapter{
    public List<UserModel> mUserModels = new ArrayList<>();
    private Context mContext;
    public int choosePosition = -1;

    public UserAdapter(Context context, List<UserModel> userModels){
        mUserModels = userModels;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mUserModels.size();
    }

    @Override
    public Object getItem(int position) {
        return mUserModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_choose_user, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_user_name);
            holder.ivHead = (CircleImageView) convertView.findViewById(R.id.iv_user_head);
            holder.ivIsUser = (ImageView) convertView.findViewById(R.id.iv_is_user);
            LoadImage.showImage(mUserModels.get(position).getImageUrl(), holder.ivHead);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(mUserModels.get(position).getUsername());
        if(position == choosePosition) {
            holder.ivIsUser.setVisibility(View.VISIBLE);
        } else {
            holder.ivIsUser.setVisibility(View.GONE);
        }
        return convertView;
    }


    public class ViewHolder{
        public TextView name;
        public CircleImageView ivHead;
        public ImageView ivIsUser;
    }
}
